package xyz.haijin.shiro.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.haijin.shiro.entity.Menu;
import xyz.haijin.shiro.entity.Role;
import xyz.haijin.shiro.service.MenuService;
import xyz.haijin.shiro.service.RoleService;
import xyz.haijin.shiro.util.MessageUtil;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("role.html")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @RequestMapping(params = "act=page")
    public String page() {
        return "role_list";
    }

    @RequestMapping(params = "act=go_edit")
    public String goEdit() {
        return "role_edit";
    }

    @RequestMapping(params = "act=list")
    @ResponseBody
    public List<Role> list() {
        return roleService.getRoleList();
    }

    @RequestMapping(params = "act=edit")
    @ResponseBody
    public Map edit(Role role) {
        if (role.getId() == null) {
            roleService.addRole(role);
        } else {
            roleService.updateRole(role);
        }
        return MessageUtil.getSuccessResult("编辑成功");
    }

    @RequestMapping(params = "act=menu_list")
    @ResponseBody
    public List<Menu> menuList() {
        return menuService.getTreeMenu();
    }

    @RequestMapping(params = "act=role_menu")
    @ResponseBody
    public List<Integer> roleMenu(Integer roleId) {

        return roleService.getRoleMenuIds(roleId);
    }

    public static void main(String[] args) {
        System.out.println();
    }
    @RequestMapping(params = "act=assign_menu")
    @ResponseBody
    public Map<String, Object> assignMenu(Integer roleId, Integer[] menuIds) {
        roleService.addRoleMenu(roleId, menuIds);
        return MessageUtil.getSuccessResult("分配成功");
    }
}
